//
//  PublishUIView.swift
//  DynamicStateLJZ
//
//  Created by kys-11 on 2019/5/12.
//  Copyright © 2019年 xl123. All rights reserved.
//

import UIKit


@objc protocol photoctionDelegate:NSObjectProtocol{
    
    func photoction(identifying:String)
    func clickBegan(textview:UITextView)//声明收齐键盘的代理事件
}
//声明代理


class PublishUIView: UIView ,UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource{


    var photodlegate: photoctionDelegate!
    //声明代理对象
    
    var pubTopimage: UIImageView!
    var pubLbale: UILabel!
    var pubtextView:UITextView!
    var pubphoto:UICollectionView!
    var pubBtton:UIButton!
    var pubclickphoto:UIImageView!
    var defaullable: UILabel!

    
    var imageArray:[UIImage] = [UIImage(named: "photo.jpg")!]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        pubcreat()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func pubcreat(){
        
        pubTopimage = UIImageView()
        pubTopimage.layer.masksToBounds = true
        pubTopimage.layer.cornerRadius = 25
        self.addSubview(pubTopimage)
        pubTopimage.image = UIImage(named: "headimage2.jpg")
        
        pubTopimage.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.width.height.equalTo(50)
        }

        pubLbale = UILabel()
        pubLbale.text = "某学生"
        pubLbale.font = UIFont(name: "Zapfino", size: 15)
        self.addSubview(pubLbale)
        
        pubLbale.snp.makeConstraints { (make) in
            make.top.equalTo(35)
            make.left.equalTo(pubTopimage.snp.right).offset(10)
            make.height.equalTo(20)
        }
        
        pubtextView = UITextView()
        pubtextView.font = UIFont(name: "SimSun", size: 20)
        pubtextView.delegate = self //设置代理
        self.addSubview(pubtextView)
        
        pubtextView.snp.makeConstraints { (make) in
            make.top.equalTo(pubTopimage.snp.bottom).offset(10)
            make.left.equalTo(pubTopimage)
            make.width.equalTo(Globalwidth)
            make.height.equalTo(150)
        }
        
        defaullable = UILabel()
        defaullable.text = "这一刻你的想法....."
        defaullable.textColor = UIColor.gray
        self.addSubview(defaullable)
        
        defaullable.snp.makeConstraints { (make) in
            make.top.equalTo(pubtextView)
            make.left.equalTo(pubtextView)
            make.height.equalTo(20)
            make.width.equalTo(300)
        }
        
        pubBtton = UIButton()
        self.addSubview(pubBtton)
        pubBtton.setTitle("选择图片", for: .normal)
        pubBtton.titleLabel?.font = UIFont(name: "Zapfino", size: 15)
        pubBtton.setTitleColor(UIColor.black, for: .normal)
        pubBtton.addTarget(self, action: #selector(photoction), for: .touchDown)
        pubBtton.snp.makeConstraints { (make) in
            make.top.equalTo(pubtextView.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.height.equalTo(15)
            make.width.equalTo(70)
            
        }
        
//        pubclickphoto = UIImageView()
//        pubclickphoto.image = UIImage(named: "photo.jpg")
//        self.addSubview(pubclickphoto)
//
//        pubclickphoto.snp.makeConstraints { (make) in
//            make.top.equalTo(pubBtton.snp.bottom).offset(10)
//            make.left.equalTo(pubBtton)
//            make.height.width.equalTo(120)
//        }
//
        let publayout = UICollectionViewFlowLayout.init()
        
        publayout.minimumLineSpacing = 5
        publayout.minimumInteritemSpacing = 10.0
        
        publayout.itemSize = CGSize(width: (Globalwidth - 10 * 4)/3, height: (Globalwidth - 8 * 4)/3)
        
        publayout.scrollDirection = .vertical
        pubphoto = UICollectionView(frame: .zero, collectionViewLayout: publayout)
        pubphoto.register(DYCollectionViewCell.self, forCellWithReuseIdentifier: "pubcell")
        
        self.addSubview(pubphoto)
        pubphoto.backgroundColor = UIColor.white
        pubphoto.delegate = self
        pubphoto.dataSource = self
        //设置代理
        pubphoto.snp.makeConstraints { (make) in
            make.top.equalTo(pubBtton.snp.bottom).offset(10)
            make.left.equalTo(pubBtton)
            make.height.equalTo(400)
            make.right.equalTo(-10)
            make.width.equalTo(Globalwidth)
        }
        
    }
   
    //更新照片数据
    func renewalimage(images:[UIImage]){
        
      
        self.imageArray = images
        
        
        if(self.imageArray.count < 9 ){
            self.imageArray.append(UIImage(named: "photo.jpg")!)
        }
        
        self.pubphoto.reloadData()
        
       pubphoto.layoutIfNeeded()
        
        let contentheight = self.pubphoto.contentSize.height
        
        
        pubphoto.snp.makeConstraints { (make) in
            make.height.equalTo(contentheight)
        }
    }
    
    
    @objc func photoction(){
    
   self.photodlegate.photoction(identifying: "photo")//触发代理事件
    
}
    
  
    //  文本变更时被调用 现在是一个字符发生变化就会调用
    func textViewDidChange(_ textView: UITextView) {
        if(pubtextView.text.count == 0){
            defaullable.isHidden = false//
            
        }else{
            defaullable.isHidden = true//隐藏默认lable
        }
    }
    
        //点击空白处收齐键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    self.photodlegate.clickBegan(textview: pubtextView)////触发代理事件
    
    }

    //返回1个分区
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    //返回分区
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    
    //返回cell的neir
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        
        print(imageArray)
        print("第三方")
        
        let cell = pubphoto.dequeueReusableCell(withReuseIdentifier: "pubcell", for: indexPath) as! DYCollectionViewCell
        
        cell.dyimageview.image = imageArray[indexPath.row]
        
        return cell
    
}
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if(indexPath.row == imageArray.endIndex-1 && imageArray.count<9){
       
             self.photodlegate.photoction(identifying: "photo")//触发代理事件
        }
    }
    
    
}

